package top.leyi.handle;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.leyi.IService.ITokenService;


import top.leyi.entity.token.JwtToken;
import top.leyi.exception.WXLoginException;
import top.leyi.utils.Result;
import top.leyi.utils.ResultGenerator;

import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/api/token")
public class TokenHandle {
    @Autowired
    ITokenService tokenService;

    /**
     * @desc 签发jwtToken
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result doPost(@RequestParam("code") String code, HttpServletResponse  response) throws WXLoginException {

        JwtToken jwtToken = tokenService.setJwtToken(code);

        Subject subject = SecurityUtils.getSubject();

        return  new Result(200,jwtToken.getPrincipal());
    }

    /**
     * @desc 签发普通token
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public Result doPut(@RequestParam("code") String code, HttpServletResponse  response) throws WXLoginException {
        return new Result(200,  tokenService.setToken(code));
    }
}
